package com.ruoyi.system.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.ResultVO;
import com.ruoyi.system.utils.CachetokenUtil;
import com.ruoyi.system.utils.YsyToken;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysVideo;
import com.ruoyi.system.service.ISysVideoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 直播Controller
 * 
 * @author l
 * @date 2020-11-18
 */
@Controller
@RequestMapping("/system/video")
public class SysVideoController extends BaseController
{
    //网页端流畅直播地址
    @Value("${ysy.RTMPSmoothAddress}")
    private String RTMPSmoothAddress;
    //网页端高清直播地址
    @Value("${ysy.RTMPHDAddress}")
    private String RTMPHDAddress;
    //移动端流畅直播地址
    @Value("${ysy.HLSSmoothAddress}")
    private String HLSSmoothAddress;
    //移动端高清直播地址
    @Value("${ysy.HLSHDAddress}")
    private String HLSHDAddress;
    //本地录像
    @Value("${ysy.LocalVideo}")
    private String LocalVideo;
    //云存储录像
    @Value("${ysy.CloudVideo}")
    private String CloudVideo;

    private String prefix = "system/video";

    @Autowired
    private YsyToken ysyToken;

    @Autowired
    private CachetokenUtil cachetokenUtil;

    @Autowired
    private ISysVideoService sysVideoService;

    @RequiresPermissions("system:video:view")
    @GetMapping()
    public String video()
    {
        return prefix + "/video";
    }

    /**
     * 查询直播列表
     */
    @RequiresPermissions("system:video:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysVideo sysVideo)
    {
        startPage();
        List<SysVideo> list = sysVideoService.selectSysVideoList(sysVideo);
        return getDataTable(list);
    }

    /**
     * 导出直播列表
     */
    @RequiresPermissions("system:video:export")
    @Log(title = "直播", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysVideo sysVideo)
    {
        List<SysVideo> list = sysVideoService.selectSysVideoList(sysVideo);
        ExcelUtil<SysVideo> util = new ExcelUtil<SysVideo>(SysVideo.class);
        return util.exportExcel(list, "video");
    }

    /**
     * 新增直播
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存直播
     */
    @RequiresPermissions("system:video:add")
    @Log(title = "直播", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysVideo sysVideo)
    {
        sysVideo.setRTMPSmoothAddress(RTMPSmoothAddress);
        sysVideo.setRTMPHDAddress(RTMPHDAddress);
        sysVideo.setHLSSmoothAddress(HLSSmoothAddress);
        sysVideo.setHLSHDAddress(HLSHDAddress);
        sysVideo.setLocalVideo(LocalVideo);
        sysVideo.setCloudVideo(CloudVideo);
        sysVideo.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(sysVideoService.insertSysVideo(sysVideo));
    }

    /**
     * 修改直播
     */
    @GetMapping("/edit/{VideoId}")
    public String edit(@PathVariable("VideoId") Integer VideoId, ModelMap mmap)
    {
        SysVideo sysVideo = sysVideoService.selectSysVideoById(VideoId);
        mmap.put("sysVideo", sysVideo);
        return prefix + "/edit";
    }

    /**
     * 修改保存直播
     */
    @RequiresPermissions("system:video:edit")
    @Log(title = "直播", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysVideo sysVideo)
    {
        sysVideo.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(sysVideoService.updateSysVideo(sysVideo));
    }

    /**
     * 删除直播
     */
    @RequiresPermissions("system:video:remove")
    @Log(title = "直播", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysVideoService.deleteSysVideoByIds(ids));
    }

    //查看直播
    @RequestMapping("/Livebroadcast")
    public String Livebroadcast(){
        return prefix+"/rtmp";
    }

    //直播回放
    @RequestMapping("/toReplay")
    public String replay(SysVideo sysVideo, ModelMap mmap){
        System.out.println(sysVideo);
//        Cache cache = cachetokenUtil.getOrAddCache("ysyToken");
//        Element element = cachetokenUtil.getYsyToken(cache);
//        System.out.println(element.getObjectValue());
        return prefix+"/revideo";
    }
}
